
    var originalAuthor = "";
    var articleId = "";

    //填充文章
    function putInArticle(data) {
        $('.zhy-article-top').html('');
        $('.zhy-article-footer').html('');
        var articleTop = $('<article-top><div class="article-title">' +
            '<h1>' + data.articleTitle + '</h1>' +
            '</div>' +
            '<div class="article-info article-info-publishDate">' +
            '<i class="am-icon-calendar"><a class="articleCategoryColor" href="#"> ' + data.publishDate + '</a></i>' +
            '</div>' +
            '<div class="article-info article-info-categories">' +
            '<i class="am-icon-folder"> <a class="articleCategoryColor" href="#">' + data.articleCategories + '</a></i>' +
            '</div>' +
            '</div></article-top><div class="article-i-say">' +
            '这是一条分割线<span class="article-i-say-me"></span>' +
            '</div>');
        $('.zhy-article-top').append(articleTop);
        $("#mdText").text(data.articleContent);
        var wordsView;
        wordsView = editormd.markdownToHTML("wordsView", {
            htmlDecode: "true", // you can filter tags decode
            emoji: true,
            taskList: true,
            tex: true,
            flowChart: true,
            // sequenceDiagram: true
        });
        var articleFooter = "<div class='end-logo'>"+
            "<i class='am-icon-btn am-success am-icon-lg'>END</i>" +
            "</div>"+
            "<div>" +
            "<ul class='post-copyright'>" +
            "<li><strong>本文作者：</strong><span id='authorFooter'>" + '熊呈' + "</span></li>" +
            "<li><strong>原文链接：</strong><span id='urlFooter'><a href='" + data.articleUrl + "'>" + data.articleUrl + "</a></span></li>" +
            "<li><strong>版权声明：</strong> 本博客所有文章除特别声明外，均采用<span id='copyRightFooter'><a href='https://creativecommons.org/licenses/by/3.0/cn/' target='_blank'> CC BY 3.0 CN协议</a></span>进行许可。转载请署名作者且注明文章出处。</li>" +
            "</ul>"+
            "</div>" +
            "<div class='article-tags'>" +

            "</div>" +
            "<hr>" +
            "<div class='two-article'>" +
            "<span class='article-last'>"+

            "</span>" +
            "<span class='article-next'>" +
            "</span>"+
            "</div>";
        $('.zhy-article-footer').append(articleFooter);

    }


    // 获取头信息，type=HEAD即可
    $.ajax({
        type: 'HEAD',
        url : window.location.href,
        async:false,
        success:function (data, status, xhr) {
            articleId = xhr.getResponseHeader("articleID");
            originalAuthor = xhr.getResponseHeader("originalAuthor");

        }

    });

    //通过文章id和原作者请求文章信息
    $.ajax({
        type:'post',
        url:'/getArticleByArticleId',
        dataType:'json',
        async:false,
        data:{
            articleID : articleId
           // originalAuthor : originalAuthor
        },
        success:function (data) {
            if(data.status == "200"){
                putInArticle(data);
            } else {
                $('.content').html('');
                var error = $('<div class="article"><div class="zhy-article-top"><div class="error">' +
                    '<img src="https://zhy-myblog.oss-cn-shenzhen.aliyuncs.com/static/img/register_success.jpg">' +
                    '<p>没有找到这篇文章哦</p>' +
                    '<p>可能不小心被博主手残删掉了吧</p>' +
                    '<div class="row">' +
                    '<a href="/">返回首页</a>' +
                    '</div>' +
                    '</div></div></div>');
                $('.content').append(error);
            }
        },
        error:function () {
            alert("返回有误");
        }
    });
    //填充评论
    articleComment();

    function articleComment() {

        //填充文章评论
        $.ajax({
            type: 'get',
            url: '/getCommentByArticleID',
            dataType: 'json',
            async: false,
            data: {
                articleID: articleId
            },
            success: function (data) {
                if (data['status'] == 200) {

                    addCommentList(data['result']);
                } else {
                    //暂无评论展示
                }
            },
            error: function () {
                alert("请求服务器时发生错误");
            }
        });
        //添加文章评论
        $("#comment").click(function () {
            var content = $("#content").val();
            var osName = getOperationSys();
            $.ajax({
                type: 'post',
                url: '/saveComment',
                dataType: 'json',

                data: {
                    articleID: articleId,
                    content: content,
                    osName: osName
                },
                success: function (data) {
                    if (data['status'] == 200) {
                        //评论成功
                        alert("评论成功");
                        window.location.reload()
                    } else {
                        //评论失败
                    }
                },
                error: function () {
                    alert("请求服务器失败")
                }
            })
        });

        function addCommentList(data) {
            var list = $(".comment-list");

            $.each(data, function (index, object) {

                if (object['replyID'] == null) {

                    var comment = "<div class='comment-info'>" +
                        "<header><img src='" + object['img'] + "'></header>" +
                        "<div class='comment-right'>" +
                        "<h3>" + object['replyName'] + "</h3>" +
                        "<div class='comment-content-header'><span><i class='glyphicon glyphicon-time'></i>" + object['time'] +
                        "</span></div>" +
                        "<p class='content' style='margin-left: -50px;  font-size: x-large;'>" + object['content'] + "</p>" +
                        "<div class='comment-content-footer' id='commentId-" + object['id'] +  "'>" +
                        "<div class='row'><div class='col-md-10'><span><i class='glyphicon glyphicon-globe'></i>" + object['osName'] + "</span></div><div class='col-md-2'><span class='reply-btn'>回复</span></div></div>" +
                        "</div>" +
                        "<div id='commentID-" + object['id'] + "' class='reply-list'></div>" +
                        "</div>" +
                        "</div>";
                    list.append(comment);
                    //填充评论回复


                    $.ajax({
                        type: 'get',
                        url: '/getReplyByCommentID',
                        dataType: 'json',
                        async: false,//改为同步执行
                        data: {
                            id: object['id']
                        },
                        success: function (data) {

                            if (data['status'] == 200) {

                                $.each(data['result'], function (index, o) {

                                    if (object['id'] == o['beReplyCommentID']) {

                                        var replyEl = "<div class='reply'><div><a href='javascript:void(0)' class='replyname'>" + o['replyName'] + "</a>:<a href='javascript:void(0)'>@" + o['beReplyName'] + "</a><span>" + o['content'] + "</span></div>"
                                            + "<p><span>" + o['time'] + "</span> <span class='reply-list-btn'>回复</span></p></div>";
                                        var commentId = "#commentID-" + object['id'];
                                        list.find(commentId).append(replyEl);

                                    }
                                })
                            }

                        }
                    });

                }

            });
            $(".reply-btn").click(function () {
                if ($(this).parent().parent().find(".replybox").length > 0) {
                    $(".replybox").remove();
                } else {
                    $(".replybox").remove();
                    replyClick($(this));
                }
            })
            $(".reply-list-btn").click(function () {
                if ($(this).parent().parent().find(".replybox").length > 0) {
                    $(".replybox").remove();
                } else {
                    $(".replybox").remove();
                    replyClick($(this));
                }
            })


        }

        function replyClick(el) {
            el.parent().parent().append("<div class='replybox'><textarea cols='80' rows='50' placeholder='输入要回复的内容......' class='mytextarea' ></textarea><span class='send'>发送</span></div>").find(".send").click(function () {
                var content = $(this).prev().val();
                if (content != "") {
                    var parentEl = $(this).parent().parent().parent().parent();
                    var obj = new Object();
                    if (el.parent().parent().hasClass("reply")) {
                        obj.beReplyName = el.parent().parent().find("a:first").text();
                        console.log(obj.beReplyName);
                    } else {
                        obj.beReplyName = parentEl.find("h3").text();
                        console.log(obj.beReplyName);
                    }
                    var id = el.parent().parent().parent().attr('id');
                    console.log(id);
                    $.ajax({
                        type: 'post',
                        url: '/saveReplyComment',
                        dataType: 'json',
                        data: {
                            articleID: articleId,
                            beReplyName: obj.beReplyName,
                            content: content,
                            replyID: id
                        },
                        success: function (data) {
                            if (data['status'] == 200) {
                                alert("评论成功成功");
                                window.location.reload()
                                //存储成功
                            }else if (data['status'] == 500){
                                alert("需要登陆才能评论哦");
                                window.location.reload()
                            }
                        }
                    });

                    $(".replybox").remove();
                    parentEl.find(".reply-list").find(".reply-list-btn:last").click(function () {
                        alert("不能回复自己");
                    });
                } else {
                    alert("输入内容不能为空");
                }
            });
        }


//获得系统名称
        function getOperationSys() {
            var OS = '';
            var OSArray = {};
            var UserAgent = navigator.userAgent.toLowerCase();
            OSArray.Windows = (navigator.platform == 'Win32') || (navigator.platform == 'Windows');
            OSArray.Mac = (navigator.platform == 'Mac68K') || (navigator.platform == 'MacPPC')
                || (navigator.platform == 'Macintosh') || (navigator.platform == 'MacIntel');
            OSArray.iphone = UserAgent.indexOf('iPhone') > -1;
            OSArray.ipod = UserAgent.indexOf('iPod') > -1;
            OSArray.ipad = UserAgent.indexOf('iPad') > -1;
            OSArray.Android = UserAgent.indexOf('Android') > -1;
            for (var i in OSArray) {
                if (OSArray[i]) {
                    OS = i;
                }
            }
            return OS;
        }
    }

